//
//  YBRecommendNewsController.m
//  Cocoachina
//
//  Created by 游兵 on 2017/9/14.
//  Copyright © 2017年 YBing. All rights reserved.
//

#import "YBRecommendNewsController.h"
#import <TFHpple.h>
#import "YBNews.h"
#import "YBNewsCell.h"
#import <MJRefresh.h>
#import "YBNewsDetailController.h"
#import "YBCycleView.h"
#import "UIView+Extension.h"
#import "Define.h"
#import "YBRecommend.h"

@interface YBRecommendNewsController ()

@property (nonatomic, strong) YBRecommend *recommend;

@end

@implementation YBRecommendNewsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    [self loadData];
    
}

- (void)setupTableView
{
    [self.tableView registerNib:[UINib nibWithNibName:@"YBNewsCell" bundle:nil] forCellReuseIdentifier:@"YBNewsCell"];
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    self.tableView.rowHeight = 100;
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadData];
    }];
    
}

- (void)setupTableHeaderView
{
    YBWeakSelf
    YBCycleView *headerView = [[YBCycleView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 250) images:[self.recommend.banner valueForKey:@"img"] currentIndex:0 didSelectForIndex:^(NSInteger index) {
        YBNewsDetailController *vc = [YBNewsDetailController new];
        vc.url = weakSelf.recommend.banner[index].url;
        [weakSelf.navigationController pushViewController:vc animated:YES];
        
    }];
    self.tableView.tableHeaderView = headerView;
}

- (void)loadData
{
    [super loadData];
    [YBHttpTool get:@"http://www.cocoachina.com" cacheType:YBCacheTypeReturnCacheDataThenLoad success:^(TFHpple *doc) {
        [self.tableView.mj_header endRefreshing];
        self.recommend = [self parseWithHpple:doc];
        [self.tableView reloadData];
        [self setupTableHeaderView];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (YBRecommend *)parseWithHpple:(TFHpple *)doc
{
    NSArray<TFHppleElement *> *elements  = [doc searchWithXPathQuery:@"//ul[@id='slide_list']"];
    TFHppleElement *listElement = [elements firstObject];
    NSArray<TFHppleElement *> *list = [listElement childrenWithTagName:@"li"];
    NSMutableArray *datas = [NSMutableArray array];
    YBRecommend *recommend = [YBRecommend new];
    [list enumerateObjectsUsingBlock:^(TFHppleElement * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        YBNews *news = [YBNews new];
        TFHppleElement *aElement = [obj firstChildWithTagName:@"a"];
        news.url = [aElement objectForKey:@"href"];
        news.title = [aElement objectForKey:@"title"];
        
        TFHppleElement *imgElement = [aElement firstChildWithTagName:@"img"];
        news.img = [imgElement objectForKey:@"src"];
        
        [datas addObject:news];
    }];
    recommend.banner = datas;
    
    
    NSArray<TFHppleElement *> *forum_c  = [doc searchWithXPathQuery:@"//div[@class='forum-c']"];
    NSArray<TFHppleElement *> *list1 = [[[forum_c firstObject] firstChildWithTagName:@"ul"] childrenWithTagName:@"li"];
    NSMutableArray *datas1 = [NSMutableArray array];
    [list1 enumerateObjectsUsingBlock:^(TFHppleElement * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        YBNews *news = [YBNews new];
        TFHppleElement *a = [obj firstChildWithTagName:@"a"];
        news.url = [a objectForKey:@"href"];
        
        TFHppleElement *img = [[a firstChildWithTagName:@"p"] firstChildWithTagName:@"img"];
        news.img = [img objectForKey:@"src"];
        
        TFHppleElement *content = [a firstChildWithTagName:@"div"];
        news.title = [content firstChildWithTagName:@"h4"].text;
        news.desc = [content firstChildWithTagName:@"p"].text;
        news.time = [content firstChildWithTagName:@"span"].text;
        [datas1 addObject:news];
    }];
    recommend.list = datas1;
    
    
    return recommend;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.recommend.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YBNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YBNewsCell" forIndexPath:indexPath];
    cell.recommend_news = self.recommend.list[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YBNewsDetailController *vc = [YBNewsDetailController new];
    vc.url = self.recommend.list[indexPath.row].url;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 9;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 9;
}

@end
